home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVO182.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  9KB  |  312 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevoki.c */
  20. /* Okidata Microline 182 printer driver for Ghostscript */
  21.  
  22. /* Contributed by Maarten Koning (smeg@bnr.ca) April 4, 1993 */
  23.  
  24. #if 0
  25.  
  26. I use this driver from Unix with the following aliases:
  27.  
  28. alias psp "gs -q -sDEVICE=oki182 -sOutputFile=\|lpr - <\!*"
  29. alias psphigh "gs -q -sDEVICE=oki182 -r144 -sOutputFile=\|lpr - <\!*"
  30.  
  31. ps. I have my printer DIP switches set to the following (as viewed
  32.     while standing in front of your printer looking down into the
  33.     config access hatch located at the top of your printer
  34.     in the centre back).
  35.  
  36. Upper  Upper   Bottom
  37. Left   Right   (at right)
  38.  
  39.  x      x        x
  40.  x       x      x
  41.  x       x       x
  42. x        x      x
  43.  x      x        x
  44.  x      x        x
  45. x        x      x
  46.  x      x        x
  47.  
  48. The upper DIP switches are on a SuperSpeed Serial
  49. card that will do 19200 baud.  I have it set at 9600
  50. baud since that seems sufficient to keep the printer
  51. busy.
  52.  
  53. The important thing is to be in 8-bit mode so that
  54. the graphics data can't match any Okidata commands
  55. (This driver sets the high bit of graphics data to 1).
  56.  
  57. #endif
  58.  
  59. #include "gdevprn.h"
  60.  
  61. /*
  62.  *  Available resolutions are 72x72 or 144x144;
  63.  *  (144x72) would be possible to do also, but I didn't bother)
  64.  */
  65.  
  66. /* The device descriptor */
  67.  
  68. private dev_proc_print_page(oki_print_page);
  69.  
  70. gx_device_printer gs_oki182_device =
  71.   prn_device(prn_std_procs, "oki182",
  72.     80,                /* width_10ths, 8.0" */
  73.     110,                /* height_10ths, 11" */
  74.     72,                /* x_dpi */
  75.     72,                /* y_dpi */
  76.     0, 0, 0, 0,            /* margins */
  77.     1, oki_print_page);
  78.  
  79. /* ------ internal routines ------ */
  80.  
  81. /* out is a pointer to an array of 7 scan lines,
  82.    lineSize is the number of bytes between a pixel and
  83.    the pixel directly beneath it.
  84.    scanBits is the number of bits in each scan line
  85.    out is a pointer to an array of column data, which
  86.    is how the Okidata wants the graphics image.
  87.  
  88.    each column of graphics data is 7 bits high and
  89.    is encoded in a byte - highest pixel in the column
  90.    is the lowest bit in the byte.  The upper bit of the
  91.    byte is set so that the okidata doesn't mistake
  92.    graphic image data for graphic commands.
  93. */
  94.  
  95. private void
  96. oki_transpose(byte *in, byte *out, int scanBits, register int lineSize)
  97. {
  98.     register bitMask = 0x80;
  99.     register byte *inPtr;
  100.     register byte outByte;
  101.  
  102.     while (scanBits-- > 0) {
  103.  
  104.         inPtr = in;
  105.  
  106.         if (*inPtr & bitMask)
  107.             outByte = 0x81;
  108.         else
  109.             outByte = 0x80;
  110.         if (*(inPtr += lineSize) & bitMask)
  111.             outByte += 0x02;
  112.         if (*(inPtr += lineSize) & bitMask)
  113.             outByte += 0x04;
  114.         if (*(inPtr += lineSize) & bitMask)
  115.             outByte += 0x08;
  116.         if (*(inPtr += lineSize) & bitMask)
  117.             outByte += 0x10;
  118.         if (*(inPtr += lineSize) & bitMask)
  119.             outByte += 0x20;
  120.         if (*(inPtr += lineSize) & bitMask)
  121.             outByte += 0x40;
  122.  
  123.         *out++ = outByte;
  124.  
  125.         if ((bitMask >>= 1) == 0) {
  126.             bitMask = 0x80;
  127.             in ++;
  128.         }
  129.     }
  130. }
  131.  
  132. /* This routine tries to compress a sequence of okidata
  133.    graphic bytes by trimming off leading and trailing
  134.    zeros.  Trailing zeros can be thrown away and leading
  135.    zeros can be replaced with a much smaller number of spaces.
  136.  
  137.    'in' is a pointer to the graphic bytes to be compressed.
  138.    origWidth is the number of bytes pointed to by 'in'.
  139.    highRes is non-zero when 144x144 mode is being used.
  140.  
  141.    numSpaces is set to the number of spaces that should
  142.    be printed before the compressed image. newWidth is
  143.    the new number of bytes that the return value of this
  144.    function points to.
  145.  
  146.    xxx - A future enhancement would be to replace long sequences
  147.    of embedded zeros with exit.graphics-<n> spaces-enter.graphics
  148. */
  149. private byte *
  150. oki_compress(byte *in, int origWidth, int highRes,
  151.             int *numSpaces, int *newWidth)
  152. {
  153.     int spaces = 0;
  154.     int columns_per_space = 6;
  155.  
  156.     byte *in_end = in + origWidth;
  157.  
  158.     /* remove trailing zeros (which are realy 0x80's) */
  159.     while (in_end > in && in_end[-1] == 0x80)
  160.         in_end --;
  161.  
  162.     if (highRes)
  163.         columns_per_space = 12;
  164.  
  165.     /* remove leading zeros that can be replaced by spaces */
  166.     while(in < in_end && in[0] == 0x80 && memcmp((char *)in,
  167.                 (char *)in + 1, columns_per_space - 1) == 0) {
  168.         spaces++;
  169.         in += columns_per_space;
  170.     }
  171.  
  172.     *numSpaces = spaces;
  173.  
  174.     /* just in case we compressed this line out of existance */
  175.     if (in_end > in)
  176.         *newWidth = in_end - in;
  177.     else
  178.         *newWidth = 0;
  179.  
  180.     return(in);
  181. }
  182.  
  183. /* Send the page to the printer. */
  184.  
  185. private int
  186. oki_print_page(gx_device_printer *pdev, FILE *prn_stream)
  187. {
  188.     int highRes = pdev->y_pixels_per_inch > 100;
  189.     int bits_per_column = 7;
  190.     int i, spaces, width;
  191.     int lcnt;
  192.  
  193.     int line_size = gdev_prn_raster((gx_device_printer *)pdev);
  194.  
  195.     byte *in = (byte *)gs_malloc(16, line_size, "oki_print_page(in)");
  196.  
  197.     byte *out1 = (byte *)gs_malloc(8, line_size, "oki_print_page(out1)");
  198.     byte *out2 = (byte *)gs_malloc(8, line_size, "oki_print_page(out2)");
  199.  
  200.     byte *out3;
  201.  
  202.     int lnum = 0;
  203.     int skip = 0;
  204.     int code = 0;
  205.  
  206.     if ( in == 0 || out1 == 0 || out2 == 0)
  207.     {    code = gs_error_VMerror;
  208.         gs_note_error(code);
  209.         goto bail;
  210.     }
  211.  
  212.     /* Initialize the printer. */
  213.     /* CAN; 72x72; left margin = 001; disable skip over perforation */
  214.     fwrite("\030\034\033%C001\033%S0", 1, 12, prn_stream);
  215.  
  216.     if (highRes) {
  217.         fwrite("\033R", 1, 2, prn_stream);
  218.         bits_per_column = 14;
  219.     }
  220.  
  221.     /* Transfer pixels to printer */
  222.     while ( lnum < pdev->height ) {
  223.  
  224.         /* Copy 1 scan line and test for all zero. */
  225.         code = gdev_prn_copy_scan_lines(pdev, lnum, in, line_size);
  226.         if ( code < 0 )
  227.             goto xit;
  228.  
  229.         /* if line is all zero, skip */
  230.         if ( in[0] == 0 && !memcmp((char *)in, (char *)in + 1,
  231.                             line_size - 1)) {
  232.             lnum++;
  233.             if (highRes)
  234.                 skip++;
  235.             else
  236.                 skip += 2;
  237.             continue;
  238.         }
  239.  
  240.         /* use fine line feed to get to the appropriate position. */
  241.         while ( skip > 127 ) {
  242.             fputs("\033%5\177", prn_stream);
  243.             skip -= 127;
  244.         }
  245.         if ( skip )
  246.             fprintf(prn_stream, "\033%%5%c",
  247.                     (char) (skip & 0xff));
  248.         skip = 0;
  249.  
  250.         /* get the rest of the scan lines */
  251.         code = gdev_prn_copy_scan_lines(pdev, lnum + 1,
  252.             in + line_size, (bits_per_column - 1) * line_size);
  253.  
  254.         if ( code < 0 )
  255.             goto xit;
  256.  
  257.         lcnt = code + 1; /* since we already grabbed one line */
  258.  
  259.         if ( lcnt < bits_per_column )
  260.             memset(in + lcnt * line_size, 0,
  261.                     (bits_per_column - lcnt) * line_size);
  262.  
  263.         if (highRes) {
  264.             oki_transpose(in, out1, pdev->width, 2 * line_size);
  265.             oki_transpose(in + line_size, out2,
  266.                         pdev->width, 2 * line_size);
  267.         } else
  268.             oki_transpose(in, out1, pdev->width, line_size);
  269.  
  270.         out3 = oki_compress(out1, pdev->width, highRes,
  271.                         &spaces, &width);
  272.  
  273.         for (i=0; i < spaces; i++)
  274.             putc(' ', prn_stream);
  275.  
  276.         fwrite("\003", 1, 1, prn_stream);
  277.         fwrite(out3, 1, width, prn_stream);
  278.  
  279.         if (highRes) {
  280.             /* exit graphics; carriage return; 1 bit line feed */
  281.             fprintf(prn_stream, "\003\002\015\033%%5%c", (char) 1);
  282.             out3 = oki_compress(out2, pdev->width, highRes,
  283.                             &spaces, &width);
  284.             for (i=0; i < spaces; i++)
  285.                 putc(' ', prn_stream);
  286.             fwrite("\003", 1, 1, prn_stream);
  287.             fwrite(out3, 1, width, prn_stream);
  288.             fprintf(prn_stream, "\003\002\015\033%%5%c", (char) 13);
  289.         } else
  290.             fwrite("\003\016\003\002", 1, 4, prn_stream);
  291.  
  292.         lnum += bits_per_column;
  293.        }
  294.  
  295.     /* Eject the page */
  296. xit:
  297.     fputc(014, prn_stream);    /* form feed */
  298.     fflush(prn_stream);
  299.  
  300. bail:
  301.     if ( out1 != 0 )
  302.         gs_free((char *)out1, 8, line_size, "oki_print_page(out1)");
  303.  
  304.     if ( out2 != 0 )
  305.         gs_free((char *)out2, 8, line_size, "oki_print_page(out2)");
  306.  
  307.     if ( in != 0 )
  308.         gs_free((char *)in, 16, line_size, "oki_print_page(in)");
  309.  
  310.     return code;
  311. }
  312.